package org.tuoren.official.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.official.domain.bo.TOrgStructureBo;
import org.tuoren.official.domain.vo.TOrgStructureVo;
import org.tuoren.official.service.ITOrgStructureService;

import java.util.List;

/**
 * 创新联合体组织架构
 *
 * @author Lion Li
 * @date 2025-03-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/official/org")
public class TOrgStructureController extends BaseController {

    private final ITOrgStructureService tOrgStructureService;

    /**
     * 查询创新联合体组织架构列表
     */
    @SaCheckPermission("official:org:list")
    @GetMapping("/list")
    public TableDataInfo<TOrgStructureVo> list(TOrgStructureBo bo, PageQuery pageQuery) {
        return tOrgStructureService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出创新联合体组织架构列表
     */
    @SaCheckPermission("official:org:export")
    @Log(title = "创新联合体组织架构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TOrgStructureBo bo, HttpServletResponse response) {
        List<TOrgStructureVo> list = tOrgStructureService.queryList(bo);
        ExcelUtil.exportExcel(list, "创新联合体组织架构", TOrgStructureVo.class, response);
    }

    /**
     * 获取创新联合体组织架构详细信息
     */
    @SaCheckPermission("official:org:query")
    @GetMapping("/detail")
    public R<TOrgStructureVo> getInfo() {
        return R.ok(tOrgStructureService.queryById(1L));
    }

    /**
     * 新增创新联合体组织架构
     */
    @SaCheckPermission("official:org:add")
    @Log(title = "创新联合体组织架构", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TOrgStructureBo bo) {
        return toAjax(tOrgStructureService.insertByBo(bo));
    }

    /**
     * 修改创新联合体组织架构
     */
    @SaCheckPermission("official:org:edit")
    @Log(title = "创新联合体组织架构", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TOrgStructureBo bo) {
        return toAjax(tOrgStructureService.updateByBo(bo));
    }

    /**
     * 删除创新联合体组织架构
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:org:remove")
    @Log(title = "创新联合体组织架构", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tOrgStructureService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 官网前台-获取创新联合体组织架构详细信息
     */
    @SaIgnore
    @GetMapping("/info")
    public R<TOrgStructureVo> getDetail() {
        return R.ok(tOrgStructureService.queryById(1L));
    }
}
